<?php

namespace App\Services;

use App\Extend\Airwallex;
use App\Extend\Payssion;
use App\Extend\Usdt;
use App\Models\OrderInfo;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class PaymentService
{
    public function createPayment($user_info, $pay_way, $order_nu, $amount, $desc, $return_url, $currency = 'USD', $attach = [])
    {
        Log::info('创建支付订单 ' . json_encode(func_get_args()));
        if (strtolower($pay_way) == "alipay") {
            $service = new Payssion();
            $result = $service->createPayment($amount, $order_nu, $desc, $currency, $return_url);
            if (empty($result) || $result['result_code'] != 200) {
                $errmsg = App::getLocale() == 'en' ? 'Order failure' : '下单失败';
                throw new \Exception($errmsg);
            }
            return [
                'amount' => $amount,
                'transaction_id' => $result['transaction']['transaction_id'],
                'transaction_platform' => 'Payssion',
                'redirect_url' => $result['redirect_url']
            ];
        }

        if (strtolower($pay_way) == "card") {
            $service = new Airwallex();
            $result = $service->createPayment($user_info, $amount, $order_nu, $desc, $currency, $return_url);
            if (empty($result)) {
                $errmsg = App::getLocale() == 'en' ? 'Order failure' : '下单失败';
                throw new \Exception($errmsg);
            }
            return [
                'amount' => $amount,
                'transaction_id' => $result['id'],
                'transaction_platform' => 'Airwallex',
                'client_secret' => $result['client_secret'],
                'redirect_url' => ''
            ];
        }

        if (strtolower($pay_way) == "usdt") {
            return [
                'amount' => $amount,
                'transaction_id' => '',
                'transaction_platform' => 'usdt',
                'redirect_url' => ''
            ];
        }

        return [];
    }

    public function confirmPayment($order_nu)
    {
        $orderInfo = OrderInfo::where('order_nu', $order_nu)->first();
        if ($orderInfo->pay_way == 'alipay' && $orderInfo->transaction_platform == 'Payssion') {
            $service = new Payssion();
            $result = $service->paymentDetails($orderInfo->order_nu, $orderInfo->transaction_id);
            if (empty($result) || $result['result_code'] != 200) {
                return [
                    'code' => -1,
                    'msg' => '付款失败'
                ];
            }
            if ($result['transaction']['state'] == 'completed') {
                return [
                    'code' => 0,
                    'msg' => '付款成功'
                ];
            }
            switch ($result['transaction']['state']) {
                case 'pending':
                    $errmsg = 'the payment has not been paid yet';
                    break;
                case 'awaiting_confirm':
                    $errmsg = 'the payment may have been paid but we have not yet received it';
                    break;
                case 'failed':
                    $errmsg = 'the payment was failed';
                    break;
                case 'cancelled':
                    $errmsg = 'the payment has been cancelled';
                    break;
                case 'expired':
                    $errmsg = 'the payment has been expired';
                    break;
                case 'rejected':
                    $errmsg = 'the payment has been rejected';
                    break;
                case 'refunded':
                    $errmsg = 'the payment has been refunded';
                    break;
                case 'chargeback':
                    $errmsg = 'the payment was chargebacked';
                    break;
                case 'blocked':
                    $errmsg = 'the payment was blocked because of unusual or suspicious activity';
                    break;
                default:
                    $errmsg = 'the payment was failed';
            }
            return [
                'code' => -1,
                'msg' => $errmsg
            ];
        }

        if ($orderInfo->pay_way == 'card' && $orderInfo->transaction_platform == 'Airwallex') {
            $result = (new Airwallex())->queryPayment($orderInfo->transaction_id);
            if (empty($result) || $result['status'] != 'SUCCEEDED') {
                return [
                    'code' => -1,
                    'msg' => '付款失败'
                ];
            }
            return [
                'code' => 0,
                'msg' => '付款成功'
            ];
        }

        if ($orderInfo->pay_way == 'usdt') {
            $transaction_quant = $orderInfo['amount'] * 1000000;
            $transaction_day = $orderInfo['create_day'];
            $result = (new Usdt())->queryTransaction($transaction_quant, $transaction_day);
            if (!empty($result)) {
                return [
                    'code' => 0,
                    'msg' => '付款成功'
                ];
            }
        }

        return [
            'code' => -1,
            'msg' => 'the payment was failed'
        ];
    }
}
